<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_category.php');
class CategoryControl
{
	var $category_m;
	function __construct()
	{
		$this->category_m = new CategoryModule();
	}


	/**
	* 根据修改或者修改类别
	* @param   int          $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   string      $cat_name       类型名称
	* @param   int         $parent_id      父类别id
	* @param   int         $sort_order     排序
	* @param   int         $type           类别类型（话题类型 还是产品类型）
	* @param   string      $cat_intro       类别短描述
	* @param   int         $cat_id    	  类别id
	* @param   string      $cat_image    	   类别image地址
	* @param   string      $cat_content    类别长描述
	* @param   int         $recommended  	 是否推荐
	* @return  int
	*/
	function  update_category($w_type,$cat_name,$parent_id,$sort_order,$type,
	$cat_intro,$cat_id = 0,$cat_image,$cat_desc,$recommended,
	$cat_thumb,$google_code_article,$google_code_guide,$google_code_buy,$google_code_oview,
	$google_code_install,$google_code_uninstall,$google_code_register)
	
	{
		$sql_s =  "SELECT parent_path FROM category  WHERE cat_id  = '$parent_id'";
		$row = $this->category_m->get_category_info($sql_s);
		if(empty($row))
		{
			$parent_path = '';
		}else
		{
			if(empty($row[0]['parent_path']))
			{
				$parent_path = $parent_id;
			}else
			{
				$parent_path = $row[0]['parent_path'].','.$parent_id;
			}
		}
		$cat_series = 1;
		if(!empty($parent_path))
		{
			$cat_series = count(explode(',',$parent_path))+1;
		}
		if(empty($cat_id))
		{
			$sql ="INSERT INTO category (cat_title ,cat_intro,sort_order ,parent_id ,cat_type ,cat_web,cat_image,
			parent_path,add_user,add_time,cat_series,cat_desc,is_recommended,cat_thumb,update_user,update_time,google_code_article,
			google_code_guide,google_code_buy,google_code_oview,google_code_install,google_code_uninstall,google_code_register) 
			VALUES ('{$cat_name}','{$cat_intro}','{$sort_order}','{$parent_id}','{$type}','{$w_type}','{$cat_image}',
			'{$parent_path}','{$_SESSION['AName']}','{$_SERVER['REQUEST_TIME']}','{$cat_series}','{$cat_desc}',
			'{$recommended}','{$cat_thumb}','{$_SESSION['AName']}','{$_SERVER['REQUEST_TIME']}','{$google_code_article}','{$google_code_guide}',
			'{$google_code_buy}','{$google_code_oview}','{$google_code_install}','{$google_code_uninstall}','{$google_code_register}') ";
		
		}else
		{
			$total = 0;
			$sql_c = "SELECT COUNT(*) AS total FROM category  WHERE cat_id =  $parent_id  AND FIND_IN_SET('$cat_id',
			parent_path)  AND  cat_web = ".$w_type;
			
			if ($res_p = $GLOBALS['db']->query($sql_c)) 
			{
				if ($row_p = $GLOBALS['db']->fetch_array($res_p)) 
				{
				$total = $row_p['total'];
				}
			}
			if($total>0)
			{
				return 2;
			}else
			{
				$sql ="UPDATE category SET cat_title = '{$cat_name}',cat_intro = '{$cat_intro}',sort_order = '{$sort_order}',
				parent_id = '{$parent_id}',cat_type = '{$type}' ,cat_image='{$cat_image}',parent_path  = '{$parent_path}',
				update_user = '{$_SESSION['AName']}' ,update_time  = '{$_SERVER['REQUEST_TIME']}' ,cat_series = '{$cat_series}'
				,cat_desc = '{$cat_desc}',is_recommended = '{$recommended}',cat_thumb = '{$cat_thumb}',google_code_article = '{$google_code_article}',
				google_code_guide = '{$google_code_guide}',google_code_buy = '{$google_code_buy}',google_code_oview = '{$google_code_oview}',
				google_code_install = '{$google_code_install}',google_code_uninstall = '{$google_code_uninstall}',google_code_register = '{$google_code_register}',
				modify_status = 1 WHERE cat_id =  '{$cat_id}'  ";	
			
				$lines = $GLOBALS['db']->query($sql);
				
				if($lines == 1 )
				{
					$this->category_m->update_category('','update',$cat_id,$parent_path);
					return 1;
				}else
				{
					return 0;
				}
			}
		}	
	
		return $this->category_m->update_category($sql);
	}
	/**
	* ajax 改变排序
	*
	* @param   int      $cat_id          类别id
	* @param   int      $sort_order      类别排序
	* @return  int
	*/
	function  change_sort($cat_id,$sort_order)
	{
		$sql = "UPDATE category SET sort_order = '$sort_order',update_user = '".$_SESSION['AName']."' ,update_time  
		= '".$_SERVER['REQUEST_TIME']."' ,modify_status = 1   WHERE cat_id= '$cat_id' ";
		return $this->category_m->change_sort($sql);
	}
	/**
	* 删除类别以及下面的子类别
	* @param   int      $cat_id          类别id
	* @return  int
	*/
	function del_category($cat_id,$w_type)
	{
		$sql = "SELECT cat_id  FROM category  WHERE FIND_IN_SET( '".$cat_id."',  `parent_path` )  AND is_delete = 0 AND  cat_web = ".$w_type;
		$query = $GLOBALS['db']->query($sql);
		$nums = $GLOBALS['db']->num_rows($query);
		if($nums > 0)
		{
			return -1;
		}else 
		{
			return $this->category_m->del_category($cat_id);
		}
	}
	/**
	* 根据条件得到分类信息
	* @param   int      $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int     $cat_id       类别id
	* @param   int      $parent_id    上级分类id
	* @param   string   $cat_name     分类名称
	* @param   int      $type         操作类型
	* @param   string   $cat_related  相关联的类别id(以逗号隔开)
	* @param   int      $cat_series   分类级数
	* @return  array
	*/
	function  get_category_info($w_type,$cat_id = 0,$parent_id = 0,$cat_name = '',$action = '',$cat_related = '',
	$cat_series = 0,$type = 0)
	{
		if($action == 'series' )
		{
			$nums = $max_series =0;
			if(!empty($cat_id))
			{
				$sql = "SELECT max(cat_series) AS max_series  FROM category  WHERE FIND_IN_SET( '".$cat_id."',  `parent_path` ) 
				AND is_delete = 0 AND  cat_web = ".$w_type;
				$query = $GLOBALS['db']->query($sql);
				$nums = $GLOBALS['db']->num_rows($query);
				if($nums > 0)
				{
					$row1 = $GLOBALS['db']->fetch_array($query);
					$max_series = $row1['max_series'] - $cat_series + 1;
				}
			}
			$sql_s = 'SELECT  cat_series  FROM category  WHERE cat_id = '.$parent_id;
			$result = $GLOBALS['db']->query($sql_s);
			$row2 = $GLOBALS['db']->fetch_array($result);
			if(($max_series+$row2['cat_series']) >3 )
			{
				return -1;
			}
			if($row2['cat_series'] == 3)
			{
				return 4;
			}
			return $row2['cat_series'];
		
		}elseif($action == 'cat_name')
		{
			if(!empty($cat_id))
			{
				$sql = "SELECT  cat_id   FROM category  WHERE cat_title = '".$cat_name."' AND cat_id != '$cat_id'  AND 
				is_delete = 0 AND cat_type = $type AND cat_web = ".$w_type;
			}else
			{
				$sql = "SELECT  cat_id   FROM category  WHERE cat_title = '".$cat_name."' AND is_delete = 0 AND cat_type = $type 
				AND cat_web = ".$w_type;
			}
		
			$query = $GLOBALS['db']->query($sql);
			return $GLOBALS['db']->num_rows($query);
		}elseif($action == 'all_cat_box')
		{
			if($type == 2)
			{
				$os_array = array("Win", "Mac");	
				$sql_os = "SELECT pr.product_os FROM `category` cat LEFT JOIN won_product ws ON cat.cat_id = ws.cat_id
				LEFT JOIN product pr ON ws.won_product_id = pr.product_id WHERE cat.cat_id ='$cat_id' AND cat.cat_web =
				'$w_type' AND pr.product_os IS NOT NULL LIMIT 1";
				$product_os = $GLOBALS['db']->getOne($sql_os);
			
				if(!empty($product_os)&& in_array($product_os,$os_array))
				{
					$product_os = $product_os == 'Win'?'Mac':'Win';
					$sql = "SELECT cat.cat_id, cat.cat_title FROM category cat LEFT JOIN won_product 
					ws ON cat.cat_id = ws.cat_id LEFT JOIN product pr ON ws.won_product_id = pr.product_id
					WHERE pr.product_os = '$product_os' AND  cat.cat_web = '$w_type' GROUP BY ws.cat_id ";
				}else
				{
					return array();
				}
			}else
			{	
				$sql = 'SELECT cat_id ,cat_title FROM category WHERE cat_type = '.$type.' AND  
				cat_web = '.$w_type.' AND is_delete = 0 AND cat_series  = 3 ';
			}
			return $this->category_m->get_category_info($sql);	
		}elseif($action == 'cat_related')
		{
			$sql = "UPDATE  category  SET cat_related = '".$cat_related."' , modify_status = 1  WHERE  cat_id = ".$cat_id;
			$GLOBALS['db']->query($sql);
		}
		else
		{
			$sql = 'SELECT cat_id ,cat_title ,cat_intro,cat_desc , sort_order ,parent_id ,cat_type 
			,cat_web,cat_image,is_recommended ,cat_thumb ,cat_series,google_code_article,google_code_guide,google_code_buy,
			google_code_oview,google_code_install,google_code_uninstall,google_code_register  FROM category WHERE  cat_web = 
			'.$w_type.' AND cat_id ='.$cat_id.' ';
			return $this->category_m->get_category_info($sql);
		}
	}
}
?>